<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

//签到
namespace app\controller;
use think\facade\Db;
class ApiSign extends ApiCommon
{
	public function initialize(){
		parent::initialize();
		$this->checklogin();
	}
	//签到页面
	public function index(){
		$signset = Db::name('signset')->where('aid',aid)->order('id desc')->find();
		$userinfo = Db::name('member')->field('id,nickname,headimg,sex,realname,tel,score,signtimes,signtimeslx,signtime,signdate')->where('id',mid)->find();
		if($this->member['signdate'] == date('Y-m-d')){
			$hassign = 1;
		}else{
			$hassign = 0;
		}
		$rdata = [];
		$rdata['signset'] = $signset;
		$rdata['userinfo'] = $userinfo;
		$rdata['hassign'] = $hassign;
        $rdata['today'] = date('Y-m-d');
        $rdata['selectedDate'] = [];
        $where = [];
        $where[] = ['aid','=',aid];
        $where[] = ['mid','=',mid];
        $sign_record = Db::name('sign_record')->where($where)->order('createtime','desc')->limit(60)->column('signdate');
        if($sign_record) {
            foreach ($sign_record as $date){
                $rdata['selectedDate'][]=['date'=>$date,'info'=>'已签'];
            }
            //{date: '2022-08-02', info: '已签'}
        }
        if($signset['display'] == 1){
        	$list = Db::name('member')->where('aid',aid)->where('signtime','>=',strtotime("yesterday"))->where('signtimeslx','>',0)->field('id,nickname,headimg,signtimes,signtimeslx,signtime')->limit(10)->order('signtimeslx desc')->select()->toArray();
            $rdata['list'] = $list;
        }
		return $this->json($rdata);
	}
    //排名数据
    public function getPaiming(){
        $pagenum = input('post.pagenum');
        if(!$pagenum) $pagenum = 1;
        $pernum = 10;

        $rdata=[];
        $rdata['datalist']=[];
        $signset = Db::name('signset')->where('aid',aid)->order('id desc')->find();
        if($signset['display'] == 1){
            $list = Db::name('member')->where('aid',aid)->where('signtime','>=',strtotime("yesterday"))->where('signtimeslx','>',0)
            ->field('id,nickname,headimg,signtimes,signtimeslx,signtime')->page($pagenum,$pernum)->order('signtimeslx desc')->select()->toArray();
            $rdata['datalist'] = $list;
        }else{
            return $this->json(['status'=>0,'msg'=>'未开启排名数据']);
        }
        $rdata['status'] = 1;
        return $this->json($rdata);
    }

	public function signin(){
        Db::startTrans();
        try {
            if(!$this->member) return $this->json(['status'=>0,'msg'=>'获取用户信息失败,请重新进入']);
            $signset = Db::name('signset')->where('aid',aid)->order('id desc')->find();
            if(!$signset || $signset['status']==0) return $this->json(['status'=>0,'msg'=>'签到活动尚未开启']);
            $this->member = Db::name('member')->lock(true)->where('aid',aid)->where('id',mid)->find();
            if($this->member['signdate'] == date('Y-m-d')) return $this->json(['status'=>0,'msg'=>'您今天已经签到过了']);
            $score = $signset['score'];
            $signtimes = $this->member['signtimes'] + 1;
            if($this->member['signdate'] == date('Y-m-d',(time()-86400))){
                $signtimeslx = $this->member['signtimeslx'] + 1;
            }else{
                $signtimeslx = 1;
            }
            $lxqdset = json_decode($signset['lxqdset'],true);
            $remark = '签到';
            $lxqd_coupon_id = 0;
            if($lxqdset){
                foreach($lxqdset as $k=>$v){
                    if($v['score']!=='' && $v['score']!==null && $signtimeslx >= $v['days']){
                        $score = $v['score'];
                        $remark = '连续签到';
                    }
                    //赠送优惠券
                    if($v['coupon_id']!=='' && $v['coupon_id']!==null && $signtimeslx >= $v['days']){
                        \app\common\Coupon::send(aid,mid,$v['coupon_id']);
                        $lxqd_coupon_id = $v['coupon_id'];
                    }
                }
            }
            $score2 = 0;
            $lxzsset = json_decode($signset['lxzsset'],true);
            $lxzs_coupon_id = 0;
            if($lxzsset){
                foreach($lxzsset as $k=>$v){
                    if($v['score']!=='' && $v['score']!==null && $signtimeslx == $v['days']){
                        $score2 = $v['score'];
                        $remark .= ' + 赠送';
                    }
                    //赠送优惠券
                    if($v['coupon_id']!=='' && $v['coupon_id']!==null && $signtimeslx == $v['days']){
                        \app\common\Coupon::send(aid,mid,$v['coupon_id']);
                        $lxzs_coupon_id = $v['coupon_id'];
                    }
                }
            }

            $totalscore = $score + $score2;
            if($totalscore > 0){
                $sdata = array();
                $sdata['aid'] = aid;
                $sdata['mid'] = mid;
                $sdata['score'] = $totalscore;
                $sdata['nickname'] = $this->member['nickname'];
                $sdata['headimg'] = $this->member['headimg'];
                $sdata['signdate'] = date('Y-m-d');
                $sdata['signtimes'] = $signtimes;
                $sdata['signtimeslx'] = $signtimeslx;
                $sdata['remark'] = $remark;
                $sdata['lxqd_coupon_id'] = $lxqd_coupon_id;
                $sdata['lxzs_coupon_id'] = $lxzs_coupon_id;
                $sdata['createtime'] = time();
                Db::name('sign_record')->insert($sdata);
                $mdata = array();
                $mdata['signdate'] = date('Y-m-d');
                $mdata['signtime'] = time();
                $mdata['signtimes'] = $signtimes;
                $mdata['signtimeslx'] = $signtimeslx;
                Db::name('member')->where('aid',aid)->where('id',mid)->update($mdata);

                \app\common\Member::addscore(aid,mid,$totalscore,'签到赠'.t('积分'));

                Db::commit();
                return $this->json(['status'=>1,'msg'=>'签到成功','scoreadd'=>$totalscore]);
            }
            Db::commit();
            return $this->json(['status'=>1,'msg'=>'签到成功','scoreadd'=>$totalscore]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->json(['status'=>0,'msg'=>'签到失败，请稍后再试']);
        }
	}
	public function signrecord(){
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mid','=',mid];
		$datalist = Db::name('sign_record')->field('id,nickname,headimg,signdate,signtimes,signtimeslx,score,remark,from_unixtime(createtime)createtime')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		if(request()->isPost()){
			return $this->json(['status'=>1,'data'=>$datalist]);
		}
		$rdata = [];
		$rdata['datalist'] = $datalist;
		return $this->json($rdata);
	}
}